import CodingMethodParameter from './CodingMethodParameter.js';

export default class CodingMethod {
    /**
     * @type {[String]}
     */
    annotations = [];
    modifier = 'public';
    /**
     * @type {CodingType}
     */
    returnType = null;
    name = '';
    /**
     * @type {[CodingMethodParameter]}
     */
    parameters = [];
    body = '';

    constructor(name) {
        this.name = name;
    }

    toString() {
        let s = '';
        for (let annotation of this.annotations) {
            if (!annotation.startsWith('@')) {
                annotation = '@' + annotation;
            }
            s += `${annotation}\n`;
        }
        let returnType = this.returnType || 'void';
        let parametersCode = this.parameters.join(', ');
        if (this.body === undefined || this.body === null) {
            s += `${returnType} ${this.name}(${parametersCode});\n`;
        } else {
            let modifier = this.modifier ? (this.modifier + ' ') : '';
            s += `${modifier}${returnType} ${this.name}(${parametersCode}) {\n`;
            s += `    ${this.body.trim()}\n`;
            s += `}\n`;
        }
        return s;
    }

    addParameter(type, name, annotation) {
        let parameter = new CodingMethodParameter(type, name);
        if (annotation) {
            parameter.annotation = annotation;
        }
        this.parameters.push(parameter);
    }

}
